{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BruTile Getting Started"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Install BruTile NuGet package"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "Installing package BruTile..done!"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Successfully added reference to package BruTile, version 2.1.2"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget:BruTile\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add usings statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "using BruTile;\n",
    "using BruTile.Predefined;\n",
    "using BruTile.Web;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create a tile source."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "//  This is an example that creates the OpenStreetMap tile source.\n",
    "var tileSource = new HttpTileSource(new GlobalSphericalMercator(0, 18),\n",
    "    \"http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png\",\n",
    "    new[] {\"a\", \"b\", \"c\"}, \"OSM\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Calculate which tiles you need"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "// the extent of the visible map changes but lets start with the whole world\n",
    "var extent = new Extent(-20037508, -20037508, 20037508, 20037508);\n",
    "var screenWidthInPixels = 400; // The width of the map on screen in pixels\n",
    "var resolution = extent.Width / screenWidthInPixels;\n",
    "var tileInfos = tileSource.Schema.GetTileInfos(extent, resolution);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Fetch the tiles from the service"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write result Col, Row, Level to console:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tile col: 0, tile row: 0, tile level: 1 , tile size 8571\n",
      "tile col: 0, tile row: 1, tile level: 1 , tile size 2818\n",
      "tile col: 1, tile row: 0, tile level: 1 , tile size 9141\n",
      "tile col: 1, tile row: 1, tile level: 1 , tile size 3314\n"
     ]
    }
   ],
   "source": [
    "foreach (var tileInfo in tileInfos)\n",
    "{\n",
    "    var tile = tileSource.GetTile(tileInfo);\n",
    "\n",
    "    Console.WriteLine(\n",
    "        $\"tile col: {tileInfo.Index.Col}, \" +\n",
    "        $\"tile row: {tileInfo.Index.Row}, \" +\n",
    "        $\"tile level: {tileInfo.Index.Level} , \" +\n",
    "        $\"tile size {tile.Length}\");\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "C#",
   "pygments_lexer": "csharp",
   "version": "8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
